Fix mktime: add normalization, O(1) algorithm, and ISO C compliance #316
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The
mktimeimplementation had three critical issues: (1) out-of-bounds array access with unnormalized month values, (2) missing ISO C required behaviors (tm_wday/tm_yday computation, field normalization), and (3) O(year) time complexity enabling DoS attacks with large year values.Changes
Normalization
normalize()function to handle field overflow/underflow before array indexingMONTH_DAYS[(*t).tm_mon]whentm_mon >= 12ISO C Compliance
calc_wday()using Zeller's congruence for day-of-week computationcalc_yday()for day-of-year computationtm_isdst = -1for unknown DST statusO(1) Time Complexity
count_leap_years()using mathematical formula:⌊y/4⌋ - ⌊y/100⌋ + ⌊y/400⌋days = (year - 1970) × 365 + leap_years + month_days + dayExample
Original prompt
mktimebehavior with ISO C standard and potential safety issues #312✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.